using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entity;
using Business;
public partial class UserControl_ProductManager : System.Web.UI.UserControl
{
    int productID = -1;
   
    eProduct product;
    
    public String type
    {
        get 
        {
            if (ViewState["type"] == null)
                return "Update";
            else
                return (String)ViewState["type"];
        }
        set
        {
            ViewState["type"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        lbdepositInvalid.Visible = false;
        lbmess.Text = "";
        if (type=="Update")
        {
            btDelete.Attributes.Add("onclick", "return confirm('Are you Sure you want to delete this Product')");
            LoadForm();
        }
    }

    private void LoadForm()
    {
        btAdd.Text = "Update";
        btDelete.Visible = true;
        product = bProduct.getProductByID(productID);


        if (productID.ToString() != lastProductID)
        {
           // Response.Write("lay cai mmoi");
            DataTable dt = bProduct_Equipment.getProduct_EquipmentByProductID(productID);
            ViewState["dt"] = dt;
            gv_equipment.DataSource = dt;
            gv_equipment.DataBind();
            lastProductID = productID.ToString();
            txtID.Text = product.productID.ToString();
            txtName.Text = product.producName;
            txtDeposit.Text = product.producSecurityDeposit.ToString();
            txtDescription.Text = product.producDescription;
        }
        else
        {
            gv_equipment.DataSource = ViewState["dt"] as DataTable;
            gv_equipment.DataBind();
           // Response.Write("lay cai cu");
        }
    }

    public String lastProductID
    {
        get 
        {
            if (string.IsNullOrEmpty((string)ViewState["lastProductID"]))
                return "-1";
            else
                return ViewState["lastProductID"] as String;
        }
        set 
        {
            ViewState["lastProductID"] = value; 
        }
    }


    public UserControl_ProductManager()
    {
    }
    public UserControl_ProductManager(String productID)
    {
        this.productID = int.Parse(productID);
        if (this.productID == -1)
        {
            this.type = "Add";
        }
    }


    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 1)
        {
            gvEquipmentList.DataSource = bEquipment.getAllEquipment();
            gvEquipmentList.DataBind();
        }
    }
    protected void btAddEquipment_Click(object sender, EventArgs e)
    {
     
        MultiView1.ActiveViewIndex = 1;
    }


    protected void gv_equipment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       // gv_equipment.
        DataTable dt = (DataTable)ViewState["dt"];
        dt.Rows.RemoveAt(e.RowIndex);
        gv_equipment.DataSource = dt;
        gv_equipment.DataBind();
    }
    protected void btReset_Click(object sender, EventArgs e)
    {
        if (type == "Update")
        {
            lastProductID = "-100";
            LoadForm();
        }
        else
        {
            lastProductID = "-100";
            txtID.Text = "";
            txtName.Text = "";
            txtDeposit.Text = "";
            txtDescription.Text = "";
            DataTable dt = ViewState["dt"] as DataTable;
            if (dt != null)
                dt.Rows.Clear();
            gv_equipment.DataSource = dt;
            gv_equipment.DataBind();
        }
    }

    protected void gvEquipmentList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow gr= gvEquipmentList.Rows[e.RowIndex];

        DataTable dt = (DataTable)ViewState["dt"];
        int equipID = int.Parse(gr.Cells[0].Text);
        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("productID",typeof(int));
            dt.Columns.Add("EquipmentID",typeof(int));
            dt.Columns.Add("Amount",typeof(int));
            dt.Columns.Add("EquipmentName");
            DataRow dr = dt.NewRow();
            dr["EquipmentID"] = equipID;
            dr["EquipmentName"] = gr.Cells[1].Text;
            dr["Amount"] = 1;
            dt.Rows.Add(dr);
            gv_equipment.DataSource = dt;
            gv_equipment.DataBind();
            MultiView1.ActiveViewIndex = 0;

            ViewState["dt"] = dt;

            return;
        }
        bool kt = false;
        
        foreach (DataRow dr1 in dt.Rows)
        {
            if (((int)dr1["EquipmentID"]) == equipID)
            {
                int i = (int)dr1["Amount"];
                dr1["Amount"] = i + 1;
                kt = true;
                break;
            }
        }
        if (!kt)
        {
            DataRow dr = dt.NewRow();
            dr["EquipmentID"] = equipID;
            dr["EquipmentName"] = gr.Cells[1].Text;
            dr["Amount"] = 1;
            dt.Rows.Add(dr);
        }

        gv_equipment.DataSource = dt;
        gv_equipment.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btA_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        float deposit;
        try
        {
            deposit = float.Parse(txtDeposit.Text.Trim());
        }
        catch (Exception)
        {

            lbdepositInvalid.Visible = true;
            return;
        }
        if (type == "Update")
        {
            product.producName = txtName.Text;
            product.producSecurityDeposit = deposit;
            product.producDescription = txtDescription.Text;
            bProduct.updateProduct(product);
            bProduct_Equipment.deleteProduct_Equipment(product.productID);

            foreach (GridViewRow gr in gv_equipment.Rows)
            {
                eProduct_Equipment pe = new eProduct_Equipment();
                pe.EquipmentID = int.Parse(gr.Cells[0].Text);
                pe.productID = product.productID;
                TextBox tb =(TextBox) gr.Cells[2].FindControl("txtAmuont");
                if (tb.Text.Trim() == "0")
                    break;
                pe.Amount = int.Parse(tb.Text.Trim());
                bProduct_Equipment.insertProduct_Equipment(pe);
            }

            lbmess.Text = "has updated!";
        }
        else
        {
            product = new eProduct();
            product.producName = txtName.Text;
            product.producSecurityDeposit = deposit;
            product.producDescription = txtDescription.Text;
            bProduct.insertProduct(product);
            
            product = bProduct.getProductByName(txtName.Text.Trim());
            //Response.Write("<br>ID:"+product.productID.ToString());
            foreach (GridViewRow gr in gv_equipment.Rows)
            {
                eProduct_Equipment pe = new eProduct_Equipment();
                pe.EquipmentID = int.Parse(gr.Cells[0].Text);
                pe.productID = product.productID;
                TextBox tb = (TextBox)gr.Cells[2].FindControl("txtAmuont");
                if (tb.Text.Trim() == "0")
                    break;
                pe.Amount = int.Parse(tb.Text.Trim());
                bProduct_Equipment.insertProduct_Equipment(pe);
            }
            //type = "Update";
            Response.Redirect("ProductManage.aspx?selectValue=Product." + product.productID);
        }
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductManage.aspx?addnew=Product");
    }
    protected void btDelete_Click(object sender, EventArgs e)
    {
        if (product != null)
        {
            bProduct.deleteProduct(product.productID);
            Response.Write("<Script>alert('you has deleted Product'); window.location.href='ProductManage.aspx'</Script>");

        }
    }
}
